package com.a01_2021.thousandstimes.saas.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.saas.entity.Union;
import com.a01_2021.thousandstimes.saas.mapper.UnionMapper;
import com.a01_2021.thousandstimes.saas.service.UnionMemberService;
import com.a01_2021.thousandstimes.saas.service.UnionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author FRQ
 * @since 2021-02-22
 */
@Service
public class UnionServiceImpl extends ServiceImpl<UnionMapper, Union> implements UnionService {

    @Resource
    UnionMapper unionMapper;

    @Resource
    UnionMemberService unionMemberService;

    /**
     * 查询全部联盟
     *
     * @return 返还List<Union>联盟列表
     */
    @Override
    public List<Union> getAllUnion() {
        QueryWrapper<Union> unionQueryWrapper = new QueryWrapper<>();
        return unionMapper.selectList(unionQueryWrapper);
    }

    /**
     * 添加联盟
     *
     * @param unionName     联盟名
     * @param des           联盟描述
     * @param hostCompanyId 举办联盟公司实例id
     * @return Union联盟实体
     */
    @Override
    public Union addUnion(String unionName, String des, long hostCompanyId) throws CustomException {
        //新建联盟
        Union union = new Union();
        union.setUnionName(unionName);
        union.setDes(des);
        union.setHostCompany(hostCompanyId);
        unionMapper.insert(union);
        //将举办用户添加到联盟中
        unionMemberService.addMember(union.getId(), hostCompanyId);
        return union;
    }

    /**
     * 通过联盟id查询联盟是否存在
     *
     * @param unionId 联盟id
     * @return boolean联盟是否存在
     */
    @Override
    public boolean isUnion(long unionId) {
        QueryWrapper<Union> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", unionId);
        int count = unionMapper.selectCount(queryWrapper);
        return count != 0;
    }
}
